/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author hernandez
 * http://efreedom.com/Question/1-1057041/Difference-Java-Php5-MD5-Hash
 */
public class Crypto {

    public static String getMD5Hash(String string) {

        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
            md5.update(string.getBytes());
            byte[] digest = md5.digest();

            string = byteArrToHexString(digest);
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(Crypto.class.getName()).log(Level.SEVERE, null, ex);
        }

        return string;
    }

    private static String byteArrToHexString(byte[] bArr) {
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < bArr.length; i++) {
            int unsigned = bArr[i] & 0xff;
            if (unsigned < 0x10) {
                sb.append("0");
            }
            sb.append(Integer.toHexString((unsigned)));

        }

        return sb.toString();
    }
    
    public static void main(String[] args){
        System.out.print(Crypto.getMD5Hash("correcthorsebatterystapple"));
        
    }
}
